package it.codelair.utilities;

import java.io.UnsupportedEncodingException;

public class MD5 {

	
	/**
	  * Calcola l'hash
	  * @param md5 la stringa in ingresso
	  * @param encoding l'encoding (es utf-8,latin1...)
	  * @return
	  * @throws UnsupportedEncodingException
	  */
	 public static String cryptMD5(String md5,String encoding) throws UnsupportedEncodingException {   
	  try {         
	   java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); 
	   String s=new String(md5.getBytes(), encoding);
	   byte[] array = md.digest(s.getBytes());        
	   StringBuffer sb = new StringBuffer();        
	   for (int i = 0; i < array.length; ++i) {  
	    String hex=Integer.toHexString((array[i] & 0xFF)+0x100).substring(1);
	    sb.append(hex);
	   }         
	   return sb.toString();     } 
	  catch (java.security.NoSuchAlgorithmException e) {    
	   throw new IllegalArgumentException("Attenzione non esiste l'algoritmo di cifratura selezionato");
	  }    
	 }
}
